﻿using Nirvana;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MeshLayerHelper : MonoBehaviour
{
    //[SerializeField]
    //[LayerMask]
    //private int layerMask;
    //[SerializeField]
    //[Layer]
    //private int layer;
    [SerializeField]
    [SortLayer]
    private int sortlayer;

    string LayerName = "Hit";

    public int order;

    public bool changechild;

    private void Start()
    {
        //InitLayer();
        //UnityEngine.Object.Destroy(this);
    }

    private void OnEnable()
    {
        InitLayer();
    }
    void InitLayer()
    {
        if (!changechild)
        {
            MeshRenderer component = GetComponent<MeshRenderer>();
            if ((bool)component)
            {
                //component.sortingLayerName = LayerName;
                component.sortingLayerID = sortlayer;
            }
        }
        else
        {
            MeshRenderer[] componentsInChildren = GetComponentsInChildren<MeshRenderer>(includeInactive: true);
            if (componentsInChildren != null)
            {
                int i = 0;
                for (int num = componentsInChildren.Length; i < num; i++)
                {
                    if (componentsInChildren[i] != null)
                    {
                        componentsInChildren[i].sortingLayerID = sortlayer;
                        //componentsInChildren[i].sortingLayerName = LayerName;
                        componentsInChildren[i].sortingOrder = order;
                    }
                }
            }
        }
    }
}
